import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class CustomRenderBox extends StatefulWidget {
  @override
  _CustomRenderBoxState createState() => _CustomRenderBoxState();
}

class _CustomRenderBoxState extends State<CustomRenderBox> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('模板'),
        ),
        body: Container(
          color: Colors.orange,
          child: MyRenderBox(
            child: FlutterLogo(
              size: 200,
            ),
          ),
        ));
  }
}

class MyRenderBox extends SingleChildRenderObjectWidget {
  const MyRenderBox({super.key, required Widget child}) : super(child: child);

  @override
  RenderObject createRenderObject(BuildContext context) {
    // TODO: implement createRenderObject
    return RenderMyRenderBox();
  }
}

class RenderMyRenderBox extends RenderBox with RenderObjectWithChildMixin {
  @override
  void performLayout() {
    // child 的layout,parentUsesSize: 是否使用父组件的size，如果是true，就用下面的size，如果是false，就用正常布局传递过来的size
    child!.layout(constraints,
        parentUsesSize:
            true); //false的话：relayout boundary 布局阻断，如果没有布局阻断，每次重构都要重新渲染，使用布局阻断，减少开销，性能提升
    // 父容器的size尺寸
    size = (child as RenderBox).size;
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    // TODO: implement paint
    context.paintChild(child!, offset);

    context.pushOpacity(offset + Offset(100, 100), 127, (context, offset) {
      context.paintChild(child!, offset);
    });
  }
}
